package org.ands.xml;

import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;

import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class DOMHelperCloneRoot extends DOMHelper{
	
	/**
	 * Construct new document by copying the documentRoot provided
	 * 
	 * @param document
	 *            the document
	 * @throws Exception
	 *             the exception
	 */
//	public DOMHelperCloneRoot(final Element documentElement, final NamespaceContext namespaceContext) throws Exception {
//		
//		assert(documentElement != null);
//		System.out.println("Request to construct document from sourceDocument, copying root element");
//		DocumentBuilder builder = DOMHelper.documentBuilderInstance();
//		_document = builder.newDocument();
//		Node newDocRoot = _document.importNode(documentElement, false);
//		
//		assert(newDocRoot != null);
//		assert(newDocRoot.getNodeName() == documentElement.getNodeName());
//		assert(newDocRoot.getNamespaceURI() == documentElement.getNamespaceURI());
//		
//		NamedNodeMap sourceAttributes =  documentElement.getAttributes();
//		NamedNodeMap newAttributes = newDocRoot.getAttributes();
//		
//		assert(sourceAttributes.getLength() == newAttributes.getLength());
//		for (int i = 0; i < sourceAttributes.getLength(); i++) {
//	        assert(sourceAttributes.item(i).getNodeName() == newAttributes.item(i).getNodeName());
//	        assert(sourceAttributes.item(i).getNodeValue() == newAttributes.item(i).getNodeValue());
//	    }
//		
//		_document.appendChild(newDocRoot);
//		
//		_nsContext = namespaceContext;
//		
//		
//	} 
	
	public DOMHelperCloneRoot(final Element documentElement, boolean deep) throws Exception {
		NamespaceContext namespaceContext = new UniversalNamespaceResolver(documentElement.getOwnerDocument());
		constructSelf(documentElement, namespaceContext, deep);
	}
	
	public DOMHelperCloneRoot(final Element documentElement, final NamespaceContext nsContext, boolean deep) throws Exception {
	
		constructSelf(documentElement, nsContext, deep);
	}

	/**
	 * Construct new document by copying the documentRoot provided
	 * 
	 * @param document
	 *            the document
	 * @throws Exception
	 *             the exception
	 */
	public void constructSelf(final Element documentElement, final NamespaceContext nsContext, boolean deep) throws Exception {
		
		assert(documentElement != null);
		System.out.println("Request to construct document from sourceDocument, copying root element");
		DocumentBuilder builder = DOMHelper.documentBuilderInstance();
		_document = builder.newDocument();
		Node newDocRoot = _document.importNode(documentElement, deep);
		
		assert(newDocRoot != null);
		assert(newDocRoot.getNodeName() == documentElement.getNodeName());
		assert(newDocRoot.getNamespaceURI() == documentElement.getNamespaceURI());
		
		NamedNodeMap sourceAttributes =  documentElement.getAttributes();
		NamedNodeMap newAttributes = newDocRoot.getAttributes();
		
		assert(sourceAttributes.getLength() == newAttributes.getLength());
		for (int i = 0; i < sourceAttributes.getLength(); i++) {
	        assert(sourceAttributes.item(i).getNodeName() == newAttributes.item(i).getNodeName());
	        assert(sourceAttributes.item(i).getNodeValue() == newAttributes.item(i).getNodeValue());
	    }
		
		_document.appendChild(newDocRoot);
		
		_nsContext = nsContext;
		
		
	} 


}
